package com.managed;

import org.hibernate.Session;
import java.util.List;
import org.hibernate.Query;

public class Helper{
	private Session session = null;
	private org.hibernate.Transaction tx;
	
	public Helper(){
		this.session = HibernateUtil.getSessionFactory().getCurrentSession();
	}
	
	public boolean iniciarTransaccion()
	{
		try
		{
			tx = session.beginTransaction();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean abortarTransaction()
	{
		try
		{
			tx.rollback();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

        public boolean confirmarTransaction()
	{
		try
		{
			tx.commit();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

        public boolean cerrarTransaction()
	{
		try
		{
			tx = null;
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

        public void salvarObjeto(Object o) throws Exception
	{
//		try
//		{
			session.save(o);
//			return true;
//		}catch(Exception e){
//			e.printStackTrace();
//			return false;
//		}
	}

        public boolean actualizarObjeto(Object o) throws Exception
	{
		try
		{
			session.saveOrUpdate(o);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
        
        public boolean eliminarObjeto(Object o)
	{
		try
		{
			session.delete(o);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

// <editor-fold defaultstate="collapsed" desc="Lista de objetos">
//        public List<Persona> getAllPersonas()
//        {
//            List<Persona> lista = null;
//
//            try {
//                Query q = session.createQuery("from Persona");
//                lista = (List<Persona>) q.list();
//                return lista;
//            } catch (Exception e) {
//                e.printStackTrace();
//                return null;
//            }
//        }// </editor-fold>
}